package cn.edu.sgu.www.mhxysy.controller.role;

import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.dto.role.RoleAttributeUpdateDTO;
import cn.edu.sgu.www.mhxysy.dto.role.RoleJiadianDTO;
import cn.edu.sgu.www.mhxysy.dto.role.RoleJiadianRenameDTO;
import cn.edu.sgu.www.mhxysy.dto.role.RoleJiadianUpdateDTO;
import cn.edu.sgu.www.mhxysy.entity.role.RoleJiadian;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.role.RoleJiadianService;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "角色加点管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/role_jiadian", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class RoleJiadianController {

	private final RoleJiadianService roleJiadianService;

	@Autowired
	public RoleJiadianController(RoleJiadianService roleJiadianService) {
		this.roleJiadianService = roleJiadianService;
	}

	@ApiOperation("重命名加点方案")
	@RequestMapping(value = "/rename", method = RequestMethod.POST)
	public JsonResult<Void> rename(@Validated RoleJiadianRenameDTO renameDTO) {
		roleJiadianService.rename(renameDTO);

		return JsonResult.success(CustomMessages.OPERATE_SUCCESS);
	}

	@ApiOperation("开启加点方案")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "roleId", value = "角色ID", required = true),
			@ApiImplicitParam(name = "jiadianSchema", value = "加点方案", required = true)
	})
	@RequestMapping(value = "/enable", method = RequestMethod.POST)
	public JsonResult<Void> enable(
			@NotBlank(message = CustomMessages.ROLE_ID_NOT_NULL) String roleId,
			@NotNull(message = CustomMessages.JIADIAN_SCHEMA_NOT_NULL) Integer jiadianSchema) {
		roleJiadianService.enable(roleId, jiadianSchema);

		return JsonResult.success("成功开启第" + jiadianSchema + "套加点方案.");
	}

	@ApiOperation("解锁加点方案")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "roleId", value = "角色ID", required = true),
			@ApiImplicitParam(name = "jiadianSchema", value = "加点方案", required = true)
	})
	@RequestMapping(value = "/unlock", method = RequestMethod.POST)
	public JsonResult<Void> unlock(
			@NotBlank(message = CustomMessages.ROLE_ID_NOT_NULL) String roleId,
			@NotNull(message = CustomMessages.JIADIAN_SCHEMA_NOT_NULL) Integer jiadianSchema) {
		roleJiadianService.unlock(roleId, jiadianSchema);

		return JsonResult.success("成功解锁第" + jiadianSchema + "套加点方案.");
	}

	@ApiOperation("修改角色加点信息")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public JsonResult<Void> update(
			@Validated RoleJiadianUpdateDTO roleJiadianUpdateDTO,
			@Validated RoleAttributeUpdateDTO roleAttributeUpdateDTO) {
		roleJiadianService.update(roleJiadianUpdateDTO, roleAttributeUpdateDTO);

		return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
	}

	@ApiOperation("查询角色加点信息")
	@RequestMapping(value = "/selectJiadian", method = RequestMethod.GET)
	public JsonResult<RoleJiadian> selectJiadian(RoleJiadianDTO jiadianDTO) {
		RoleJiadian roleJiadian = roleJiadianService.selectJiadian(jiadianDTO);

		return JsonResult.success(null, roleJiadian);
	}

	@ApiOperation("通过角色ID查询角色加点")
	@RequestMapping(value = "/selectByRoleId", method = RequestMethod.GET)
	public JsonResult<List<RoleJiadian>> selectByRoleId(
			@ApiParam(name = "roleId", value = "角色ID", required = true)
			@NotBlank(message = CustomMessages.ROLE_ID_NOT_NULL) String roleId) {
		List<RoleJiadian> list = roleJiadianService.selectByRoleId(roleId);

		return JsonResult.success(null, list);
	}

}